package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cqjtu.eecs.entity.Project;
import com.cqjtu.eecs.entity.Requirement;
import com.cqjtu.eecs.entity.TrainingGoal;
import com.cqjtu.eecs.entity.params.requirement.RequirementAddParams;
import com.cqjtu.eecs.entity.params.requirement.RequirementUpdateParams;
import com.cqjtu.eecs.entity.params.trainingGoal.TrainingGoalAddParams;
import com.cqjtu.eecs.entity.params.trainingGoal.TrainingGoalUpdateParams;
import com.cqjtu.eecs.entity.vo.RequirementVO;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.IProjectService;
import com.cqjtu.eecs.service.IRequirementService;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 毕业要求 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/requirement")
@Api(tags = "年级毕业要求")
public class RequirementController {

    @Autowired
    private IRequirementService requirementService;
    @Autowired
    private IProjectService projectService;

    @GetMapping("getRequirementByCodeAndGrade/{majorCode}/{year}")
    @ApiOperation(value = "通过专业代码和年级获取毕业要求")
    public List<RequirementVO> getRequirement(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year) {
        Assert.notBlank(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notBlank(year, new ParamsErrorException("年级不能为空"));
        Project project= projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return null;
        String pid=project.getPid().toString();
        return requirementService.getRequirement(pid);
    }


    @ApiOperation(value = "新增一条毕业要求")
    @PostMapping("addRequirement")
    public Result<Object> addRequirement(@Valid @RequestBody RequirementAddParams requirementAddParams)
    {
        Requirement requirement=new Requirement();
        requirement.setRequirementCode(requirementAddParams.getRequirementCode());
        requirement.setRequirementDesc(requirementAddParams.getRequirementDesc());
        requirement.setPreRequirementId(requirementAddParams.getPreRequirementId());
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",requirementAddParams.getMajorCode()).eq("grade",requirementAddParams.getGrade()));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();
        requirement.setPid(pid);
        List<Requirement> requirements=requirementService.list(new QueryWrapper<Requirement>().eq("pid",pid));
        for (Requirement i:requirements) {
            if(i.getRequirementCode().equals(requirementAddParams.getRequirementCode()))
                return Result.error(-1,"毕业要求代码重复");
        }
        int t=1;
        if(!requirement.getPreRequirementId().equals(""))
        {
            t=0;
            for(Requirement i:requirements){
                if(i.getRequirementCode().equals(requirement.getPreRequirementId()))
                {
                    t=1;
                    break;
                }
            }
        }
        if (t==0)
            return  Result.error(-1,"该上级毕业要求不存在");
        if (requirementService.save(requirement))
            return Result.success();
        return Result.error();
    }

    @ApiOperation("删除一条毕业要求")
    @DeleteMapping("/deleteRequirement/{majorCode}/{year}/{requirementCode}")
    public Result<Object> deleteRequirement(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year,@PathVariable("requirementCode")String requirementCode)
    {
        Assert.notBlank(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notBlank(year, new ParamsErrorException("年级不能为空"));
        Assert.notBlank(requirementCode,new ParamsErrorException("毕业要求代码不能为空"));
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();

        if (requirementService.remove(new LambdaQueryWrapper<Requirement>().eq(Requirement::getPid, pid).eq(Requirement::getRequirementCode,requirementCode)))
        {
            requirementService.remove(new LambdaQueryWrapper<Requirement>().eq(Requirement::getPid,pid).eq(Requirement::getPreRequirementId,requirementCode));
            return Result.success();
        }
        return Result.error(-1,"该毕业要求代码不存在");
    }


    @ApiOperation("更新一条毕业要求")
    @PutMapping("/updateRequirement")
    public Result<Object> updateRequirement(@Valid @RequestBody RequirementUpdateParams requirementUpdateParams)
    {
        Requirement requirement=new Requirement();
        requirement.setRequirementCode(requirementUpdateParams.getRequirementCode());
        requirement.setRequirementDesc(requirementUpdateParams.getRequirementDesc());
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",requirementUpdateParams.getMajorCode()).eq("grade",requirementUpdateParams.getGrade()));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();
        requirement.setPid(pid);

        if (requirementService.update(new LambdaUpdateWrapper<Requirement>()
                .eq(Requirement::getPid, pid)
                .eq(Requirement::getRequirementCode, requirement.getRequirementCode())
                .set(requirement.getRequirementDesc() != null, Requirement::getRequirementDesc, requirement.getRequirementDesc())
        ))
            return Result.success();
        return Result.error(-1,"该毕业要求代码不存在");
    }

    @ApiOperation("更新多条毕业要求")
    @PutMapping("/updateRequirementList")
    public Result<Object> updateRequirementList(@Valid @RequestBody List<RequirementUpdateParams> requirementUpdateParamsList)
    {
        for (RequirementUpdateParams i:requirementUpdateParamsList
             ) {
            Requirement requirement=new Requirement();
            requirement.setRequirementCode(i.getRequirementCode());
            requirement.setRequirementDesc(i.getRequirementDesc());
            Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",i.getMajorCode()).eq("grade",i.getGrade()));
            if(project == null)
                return Result.error(-1,"未查到培养方案");
            int pid=project.getPid();
            requirement.setPid(pid);

            if (requirementService.update(new LambdaUpdateWrapper<Requirement>()
                    .eq(Requirement::getPid, pid)
                    .eq(Requirement::getRequirementCode, requirement.getRequirementCode())
                    .set(requirement.getRequirementDesc() != null, Requirement::getRequirementDesc, requirement.getRequirementDesc())
            ))
                continue;
            else
                return Result.error();
        }
        return Result.success();
    }
}
